import Vue from 'vue'
import VueRouter from 'vue-router'
import {
    exist
} from "../axios/api";
Vue.use(VueRouter)

const routes = [
    // 更换  路由 LiyingqiTravelCase名字时候记得 vue.config.js里面的publicPath 也需要换偶
    // 案例一
    {
        path: '/',
        component: () =>
            import ('../views/index.vue'),
        children: [{
                path: '/',
                component: () =>
                    import ('../views/apply.vue'),
            },
            {
                path: '/declare',
                component: () =>
                    import ('../views/declare.vue'),
            },
            {
                path: '/user_authority',
                component: () =>
                    import ('../views/user_authority.vue'),
            },
            {
                path: '/authority',
                component: () =>
                    import ('../views/authority.vue'),
            },
            {
                path: '/chuangjian',
                name: "chuangjian",
                component: () =>
                    import ('../views/chuangjian.vue'),
            },
            {
                path: '/dels',
                name: "dels",
                component: () =>
                    import ('../views/dels.vue'),
            },
            {
                path: '/fenpei',
                component: () =>
                    import ('../views/fenpei.vue'),
            },
            {
                path: '/awards',
                component: () =>
                    import ('../views/awards.vue'),
            },
            {
                path: '/shenqing',
                component: () =>
                    import ('../views/shenqing1.vue'),
            },
            {
                path: '/shenqing1',
                component: () =>
                    import ('../views/shenqing.vue'),
            },
            {
                path: '/tongji',
                component: () =>
                    import ('../views/tongji.vue'),
            },

            {
                path: '/pingshen',
                component: () =>
                    import ('../views/pingshen.vue'),
            },
            {
                path: '/fenzu',
                component: () =>
                    import ('../views/fenzu.vue'),
            },
            {
                path: '/tongjis',
                component: () =>
                    import ('../views/tongjis.vue'),
            },
            {
                path: '/fenpeis',
                component: () =>
                    import ('../views/fenpeis.vue'),
            },
            {
                path: '/delse',
                component: () =>
                    import ('../views/delse.vue'),
            },
        ]
    },
    {
        path: '/login',
        name: 'login/',
        component: () =>
            import ('../views/login.vue'),

    },
]
const router = new VueRouter({
    mode: '',
    base: '/',
    routes,
})
router.beforeEach((to, from, next) => {
    const token = sessionStorage.getItem('Token1')
    if (token && to.path !== '/login') {
        // 有token 但不是去 login页面 通过
        next()
    } else if (token && to.path === '/login') {
        // 有token 但是去 login页面 不通过 重定向到首页
        next('/')
    } else if (!token && to.path !== '/login') {
        // 没有token 但不是去 login页面 不通过（未登录不给进入）
        console.log(to.path);
        if (to.path === '/enroll') {
            // 没有token 但不是去 login页面 不通过（未登录不给进入）
            next(true)
        } else {
            next('/login')
        }
    } else {
        // 剩下最后一种 没有token 但是去 login页面 通过
        next()
    }
})

export default router